﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MyRoguelike.Screens.Manager
{
    static class ScreenManager
    {
        static private Dictionary<string, Screen> _lstScreens = new Dictionary<string, Screen>();

        static private Screen _screenActive;
        static private Screen _screenPrevious;
        private static Microsoft.Xna.Framework.Game _game;

        static public Microsoft.Xna.Framework.Game Game
        {
            get { return _game; }
        }

        /// <summary>
        /// Initialize screen manager
        /// </summary>
        /// <param name="game">Game</param>
        static public void Initialize(Microsoft.Xna.Framework.Game game)
        {
            _game = game;
        }

        /// <summary>
        /// Update active screen
        /// </summary>
        /// <param name="gameTime">GameTime</param>
        static public void Update(GameTime gameTime)
        {
            if (_screenActive != null)
                _screenActive.Update(gameTime);
        }

        /// <summary>
        /// Draw active screen
        /// </summary>
        /// <param name="spriteBatch">SpriteBatch</param>
        static public void Draw(SpriteBatch spriteBatch)
        {
            if (_screenActive != null)
                _screenActive.Draw(spriteBatch);
        }

        /// <summary>
        /// Add new screen
        /// </summary>
        /// <param name="name">Screen name</param>
        /// <param name="screen">Screen</param>
        static public void AddScreen(string name, Screen screen)
        {
            if (!_lstScreens.ContainsKey(name))
                _lstScreens.Add(name, screen);
        }

        /// <summary>
        /// Returns specified screen
        /// </summary>
        /// <param name="name">Screen name</param>
        /// <returns>Screen</returns>
        static public Screen Get(string name)
        {
            return _lstScreens.ContainsKey(name) ? _lstScreens[name] : null;
        }

        /// <summary>
        /// Setting specified screen as an active
        /// </summary>
        /// <param name="name">Screen name</param>
        static public void Goto(string name)
        {
            if (_lstScreens.ContainsKey(name))
            {
                if (_screenActive != null)
                {
                    _screenPrevious = _screenActive;
                    _screenPrevious.Shutdown();
                    _screenPrevious.UnloadContent();
                }

                _screenActive = _lstScreens[name];
                _screenActive.LoadContent();
                _screenActive.Initialize();
            }
        }

        /// <summary>
        /// Delete specified screen
        /// </summary>
        /// <param name="name">Screen name</param>
        static public void DeleteScreen(string name)
        {
            if (!_lstScreens.ContainsKey(name))
            {
                if (_screenActive != _lstScreens[name])
                    _lstScreens.Remove(name);
                else
                {
                    _lstScreens[name].Shutdown();
                    _lstScreens[name].UnloadContent();
                    _lstScreens.Remove(name);
                }

            }
        }
    }
}
